package com.bcu.team.comment.service.serviceImpl;

import com.bcu.team.comment.entity.Message;
import com.bcu.team.comment.entity.User;
import com.bcu.team.comment.mapper.UserMapper;
import com.bcu.team.comment.service.UserService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public Message<User> queryUser(Integer userId) {
        return Message.success(null).add(userMapper.selectByPrimaryKey(1)) ;
    }

    @Override
    public Message<List<User>> getAll() {
        return Message.success(null).add(userMapper.selectAllUser());
    }

    @Override
    public Message<?> addUser(User user) {
        return userMapper.insertSelective(user) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<?> delUser(Integer userId) {
        return userMapper.deleteByPrimaryKey(userId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<?> updateUser(User user) {
        return userMapper.updateByPrimaryKeySelective(user) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message userLogin(Integer userId, String userPassword) {
        User u = userMapper.selectByPrimaryKey(userId);

        if(u ==null){
            return Message.fail("用户存在");
        }else {
            if(userPassword.equals(u.getUserPwd())){
                return Message.success("登录成功").add(u);
            }
            else{
                return Message.fail("密码错误");
            }
        }

    }
}
